//=============================================================================
// BiggerAnimationFrame.js
//=============================================================================

/*:
 * @plugindesc 解除192*192的Sprite限制大小。
 * @author
 *
 * @help 在設置動畫時，其命名規則如下：
 * 
 * $動畫名[寬,高]     //ex. $物理打擊[192,192]
 *
 * @param
 * @desc
 * @default
 * @type
 */

(function () {
	var _Sprite_Animation_initMembers = Sprite_Animation.prototype.initMembers;
	Sprite_Animation.prototype.initMembers = function () {
		_Sprite_Animation_initMembers.call(this);
		this.cellWidth = 192;
		this.cellHeight = 192;
	}

	var _DataManage_onLoad = DataManager.onLoad;
	DataManager.onLoad = function (object) {
		_DataManage_onLoad.call(this, object);
		if (object === $dataAnimations) {
			for (animation of $dataAnimations) {
				if (!animation) {
					continue;
				}
				var name = animation.name;
				if (name.charAt(0) == "$") {
					var args = JSON.parse(name.substring(name.indexOf("["), name.indexOf("]") + 1));
					animation.large = true;
					animation.cellWidth = args[0];
					animation.cellHeight = args[1];
				}
			}
		}
	}

	var origin_updateAllCellSprites = `function(frame){for(vari=0;i<this._cellSprites.length;i++){varsprite=this._cellSprites[i];if(i<frame.length){this.updateCellSprite(sprite,frame[i]);}else{sprite.visible=false;}}}`;
	if (origin_updateAllCellSprites != Sprite_Animation.prototype.updateAllCellSprites.toString().replace(/\s/g, "")) {
		console.warn("本插件是覆寫Sprite_Animation.prototype.updateAllCellSprites，若與原本的函數不相同可能會造成出錯，可以考慮尋找插件衝突或者將本插件順位上移");
	}

	var _Sprite_Animation_updateAllCellSprites = Sprite_Animation.prototype.updateAllCellSprites;
	Sprite_Animation.prototype.updateAllCellSprites = function (frame) {
		if (this._animation.large) {
			for (var i = 0; i < this._cellSprites.length; i++) {
				var sprite = this._cellSprites[i];
				if (i < frame.length) {
					this.updateLargeCellSprite(sprite, frame[i]);
				} else {
					sprite.visible = false;
				}
			}
		} else {
			_Sprite_Animation_updateAllCellSprites.call(this, frame);
		}
	};

	Sprite_Animation.prototype.updateLargeCellSprite = function (sprite, cell) {
		var pattern = cell[0];
		if (pattern >= 0) {
			var sx = pattern % 5 * this._animation.cellWidth;
			var sy = Math.floor(pattern % 100 / 5) * this._animation.cellHeight;
			var mirror = this._mirror;
			sprite.bitmap = pattern < 100 ? this._bitmap1 : this._bitmap2;
			sprite.setFrame(sx, sy, this._animation.cellWidth, this._animation.cellHeight);
			sprite.x = cell[1];
			sprite.y = cell[2];
			sprite.rotation = cell[4] * Math.PI / 180;
			//sprite.scale.x = cell[3] / 100;
			sprite.scale.x = cell[3] / 100 * 192 / this._animation.cellWidth; //自動按寬度修正比例
			

			if (cell[5]) {
				sprite.scale.x *= -1;
			}
			if (mirror) {
				sprite.x *= -1;
				sprite.rotation *= -1;
				sprite.scale.x *= -1;
			}

			//sprite.scale.y = cell[3] / 100;
			sprite.scale.y = cell[3] / 100 * 192 / this._animation.cellWidth; //自動按寬度修正比例
			sprite.opacity = cell[6];
			sprite.blendMode = cell[7];
			sprite.visible = true;

			// 我方角色動畫放大
			if (this.isOnActor()){
				const zoomRate = 1.5;
				sprite.x *= zoomRate;
				sprite.y *= zoomRate;
				sprite.scale.x *= zoomRate;
				sprite.scale.y *= zoomRate;
			}
			
		} else {
			sprite.visible = false;
		}
	};
})();